<?php
namespace Api\Controller;
use Think\Controller;
class ResourcevideoController extends ApiBaseController{
	
	function callback(){
		\Think\Log::record(implode("", $_GET), 'INFO');
		\Think\Log::record(implode("", $_POST), 'INFO');
		
		$contents = file_get_contents('php://input');
		\Think\Log::record("腾讯云回调参数：".$contents, 'INFO');
		try {
			$data = json_decode($contents, true);
			$fileId = $data['data']['fileId'];
			
			$resource_model = M('vrnews1.resource_library',NULL);
			
// 			$imgUrls = $data['data']['image_video']['imgUrls'];
			$videoUrls = $data['data']['playSet'];
			$thumbnailUrl = $data['data']['coverUrl'];
// 			foreach ($imgUrls as $v){
// 				if(1 == $v['id'] && '640' == $v['vwidth']){
// 					$thumbnailUrl = $v['url'];
// 					break;
// 				}
// 			}
			$videoUrl = "";
			$height = "";
			$weight = "";
			$size = "";
			$add_data = array();
			foreach ($videoUrls as $v){
				if(30 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
					$add_data['url'] = $v['url'];
					$add_data['height'] = $v['vheight'];
					$add_data['width'] = $v['vwidth'];
					$add_data['size'] = $v['vbitrate'];
				}
				if(20 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
					$add_data['url1'] = $v['url'];
					$add_data['height1'] = $v['vheight'];
					$add_data['width1'] = $v['vwidth'];
					$add_data['size1'] = $v['vbitrate'];
				}
				if(10 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
					$add_data['url2'] = $v['url'];
					$add_data['height2'] = $v['vheight'];
					$add_data['width2'] = $v['vwidth'];
					$add_data['size2'] = $v['vbitrate'];
				}
			}
					
			if(empty($add_data['url'])){
				$add_data['url'] = $add_data['url1'];
			}
			//时长
			$duration = $this->changeTimeType($data['data']['duration']);
			//更新视频表
			$add_data['thumbnail_url'] = $thumbnailUrl;
			$add_data['duration'] = $duration;
			//is_vr 1 是vr 0不是  category 1 普通视频 2 vr视频
			$add_data['category'] = '1';
			$add_data['create_time'] = date('Y-m-d H:i:s',time());
			$add_data['status'] = '2';
				
			$resource_model->where("file_id = '$fileId'")->data($add_data)->save();
				
		}catch (Exception $e){
			\Think\Log::record($e->getMessage(), 'WARN');
			\Think\Log::record($e->getTraceAsString(), 'WARN');
		}
		exit();
	}
	
	function changeTimeType($seconds) { 
	    if ($seconds > 3600) { 
	        $hours = intval($seconds / 3600); 
	        $minutes = $seconds % 3600; 
	        $time = $hours . ":" . gmstrftime('%M:%S', $minutes); 
	    } else { 
	        $time = gmstrftime('%H:%M:%S', $seconds); 
	    } 
	    return $time; 
	}
	
	
	function get_video(){
		$config = array('SecretId'       => C("API_SecretId"),
				'SecretKey'      => C("API_SecretKey"),
				'RequestMethod'  => 'GET',
				'DefaultRegion'  => C("API_DefaultRegion"));
		vendor("QcloudApi.QcloudApi");
		$service = \QcloudApi::load(\QcloudApi::MODULE_VIDEO, $config);
		//要输入的参数
		$package = array(
				'fileId' => '14651978969340560036'
		);
		$a = $service->DescribeVodPlayUrls($package);
		\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
		print $service->getLastRequest();
		print_r($a);
	}
	
}